package cn.dt.ymcc.listener;

import cn.dt.ymcc.domain.CourseOrder;
import cn.dt.ymcc.domain.CourseOrderItem;
import cn.dt.ymcc.dto.PayDto;
import cn.dt.ymcc.service.ICourseOrderItemService;
import cn.dt.ymcc.service.ICourseOrderService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQTransactionListener(txProducerGroup ="sever-order")
public class OrderRocketMQListener implements RocketMQLocalTransactionListener {
    @Autowired
    private ICourseOrderService courseOrderService;
    @Autowired
    private ICourseOrderItemService courseOrderItemService;
    /**
     * 执行本地事务（操作数据库都在这里执行）
     * @param message t_prder_pay的数据
     * @param org 存的是主订单和子订单需要保存到数据库中的数据
     * @return
     */
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object org) {
        if(org==null){
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        try {
            CourseOrder order = (CourseOrder) org;
            CourseOrderItem item = order.getItem();
            item.setOrderId(order.getId());
            courseOrderItemService.insert(item);
            courseOrderService.insert(order);
        } catch (Exception e) {
            e.printStackTrace();
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        return RocketMQLocalTransactionState.COMMIT;
    }

    /**
     * 检查本地事务
     * 通过查询主表和字表的订单号进行查询
     * courseOrderService
     * courseOrderItemService
     * @param message(进行了jsonString化，要转成对象还要转换成dto)
     * @return
     */
    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        byte[] bytes = (byte[]) message.getPayload();
        String dtoStr = new String(bytes, StandardCharsets.UTF_8);
        PayDto payDto = JSONObject.parseObject(dtoStr,PayDto.class);
        if(payDto==null){
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        try {
            String orderNo = payDto.getOrderNo();
            Wrapper<CourseOrder> orderWrapper = new EntityWrapper<>();
            orderWrapper.eq("order_no",orderNo);
            courseOrderService.selectOne(orderWrapper);
            Wrapper<CourseOrderItem> orderItemWrapper= new EntityWrapper<>();
            orderItemWrapper.eq("order_no",orderNo);
            courseOrderItemService.selectOne(orderItemWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        return RocketMQLocalTransactionState.COMMIT;
    }
}
